package com.xinqi.modules.sys.dict.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.sys.dict.domain.DictGradeEntity;
import com.xinqi.modules.sys.dict.dto.req.DictGradeQueryDTO;
import com.xinqi.modules.sys.dict.dto.rsp.DictGradeResultDTO;
import com.xinqi.modules.sys.dict.manager.DictGradeManager;
import com.xinqi.modules.sys.dict.mapper.DictGradeMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 字典-年级管理层实现
 *
 * @author: linkai
 * @date: 2023/05/03
 */
@Service
@RequiredArgsConstructor
public class DictGradeManagerImpl extends BaseServiceImpl<DictGradeMapper, DictGradeEntity> implements DictGradeManager {

    /**
     * 自定义分页查询
     */
    @Override
    public Page<DictGradeResultDTO> findPage(Page<Object> page, DictGradeQueryDTO query) {
        return this.baseMapper.findPage(page, query);
    }

    /**
     * 自定义列表查询
     */
    @Override
    public List<DictGradeResultDTO> findList(DictGradeQueryDTO query) {
        return this.baseMapper.findList(query);
    }

    /**
     * 根据id查询 字典-年级数据
     */
    @Override
    public DictGradeEntity findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return getById(id);
    }

    /**
     * 根据id删除 字典-年级数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return this.removeById(id);
    }

    @Override
    public Boolean isExistName(String name, Long oId) {
        LambdaQueryWrapper<DictGradeEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DictGradeEntity::getName, name);
        wrapper.ne(oId != null, DictGradeEntity::getId, oId);
        return count(wrapper) > 0;
    }

}
